package com.example.tomatotodo.api;

import com.example.tomatotodo.BuildConfig;
import com.example.tomatotodo.util.RecordUtil;
import com.google.gson.Gson;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiClient {
    private static Retrofit retrofit;

    public static Retrofit getClient() {
        if (retrofit == null) {
            Gson gson = RecordUtil.getGsonInstance();

            // 设置网络超时时间
            OkHttpClient client = new OkHttpClient.Builder()
                    .connectTimeout(5, TimeUnit.SECONDS)  // 连接超时5秒
                    .readTimeout(5, TimeUnit.SECONDS)     // 读取超时5秒
                    .writeTimeout(5, TimeUnit.SECONDS)    // 写入超时5秒
                    .build();

            retrofit = new Retrofit.Builder()
                    .baseUrl(BuildConfig.API_BASE_URL) // 注意：结尾的斜杠要加上
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();
        }
        return retrofit;
    }
}
